﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Calendar
{
    public static TimePeriod TodayFullDay()
    {
        return TimePeriod.CreatePeriodFor(DateTime.Today);
    }

    public static DateTime Tomorrow()
    {
        return DateTime.Now.AddDays(1);
    }

    public static DateTime Yesterday()
    {
        return DateTime.Now.AddDays(-1);
    }

    public static DateTime Next(DayOfWeek nextDayOfWeek)
    {
        var currentDate = DateTime.Today;
        do
        {
            currentDate = currentDate.AddDays(1);
        }
        while (currentDate.DayOfWeek != nextDayOfWeek);

        return currentDate;
    }

    public static DateTime Last(DayOfWeek lastDayOfWeek)
    {
        var currentDate = DateTime.Today;
        do
        {
            currentDate = currentDate.AddDays(-1);
        }
        while (currentDate.DayOfWeek != lastDayOfWeek);

        return currentDate;
    }

    public static TimePeriod LastWeek()
    {
        var start = DateTime.Now.AddWeek(-1).FirstDayOfWeek().BeginningOfDay();
        var end = start.LastDayOfWeek().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod ThisWeek()
    {
        return new TimePeriod(DateTime.Today.FirstDayOfWeek().BeginningOfDay(), DateTime.Today.LastDayOfWeek().EndingOfDay());
    }

    public static TimePeriod NextWeek()
    {
        var start = DateTime.Now.AddWeek(1).FirstDayOfWeek().BeginningOfDay();
        var end = start.LastDayOfWeek().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod CurrentWorkWeek()
    {
        return new TimePeriod(DateTime.Today.FirstWorkDayOfWeek().BeginningOfDay(), DateTime.Today.LastWorkDayOfWeek().EndingOfDay());
    }

    public static TimePeriod LastMonth()
    {
        var start = DateTime.Now.AddMonths(-1).FirstDayOfMonth().BeginningOfDay();
        var end = start.LastDayOfMonth().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod ThisMonth()
    {

        return new TimePeriod(start: DateTime.Today.FirstDayOfMonth().BeginningOfDay(), 
                              end: DateTime.Today.LastDayOfMonth().EndingOfDay());
    }

    public static TimePeriod NextMonth()
    {
        var start = DateTime.Now.AddMonths(1).FirstDayOfMonth().BeginningOfDay();
        var end = start.LastDayOfMonth().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod LastQuartile()
    {
        var start = DateTime.Now.AddMonths(-6).FirstDayOfMonth().BeginningOfDay();
        var end = start.AddMonths(3).LastDayOfMonth().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod ThisQuartile()
    {
        var start = DateTime.Now.AddMonths(-3).FirstDayOfMonth().BeginningOfDay();
        var end = start.AddMonths(3).LastDayOfMonth().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod NextQuartile()
    {
        var start = DateTime.Now.AddMonths(6).FirstDayOfMonth().BeginningOfDay();
        var end = start.AddMonths(3).LastDayOfMonth().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod LastSemester()
    {
        var start = DateTime.Now.AddMonths(-9).FirstDayOfMonth().BeginningOfDay();
        var end = start.AddMonths(6).LastDayOfMonth().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod ThisSemester()
    {
        var start = DateTime.Now.AddMonths(-6).FirstDayOfMonth().BeginningOfDay();
        var end = start.AddMonths(6).LastDayOfMonth().EndingOfDay();

        return new TimePeriod(start, end);
    }

    public static TimePeriod NextSemester()
    {
        var start = DateTime.Now.AddMonths(9).FirstDayOfMonth().BeginningOfDay();
        var end = start.AddMonths(6).LastDayOfMonth().EndingOfDay();

        return new TimePeriod(start, end);
    }
}